package solutions.leetcode.easy;

import solutions.lib.BaseSolution;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/">1779.找到最近的有相同X或Y坐标的点</a>
 */
public class Solution1779 extends BaseSolution {

    public int nearestValidPoint(int x, int y, int[][] points) {
        int i = -1, min = Integer.MAX_VALUE;
        for (int j = 0; j < points.length; j++) {
            if (points[j][0] == x || points[j][1] == y) {
                int dis = Math.abs(points[j][0] - x) + Math.abs(points[j][1] - y);
                if (dis < min) {
                    i = j;
                    min = dis;
                }
            }
        }
        return i;
    }

    public static void main(String[] args) {
        Solution1779 solution = new Solution1779();
    }
}